using System;
using System.Collections.Generic;
using System.Text;
using Net.RequestHandle;

namespace MeetFuture.MRC
{
    public enum MainStatus
    {
        None = 0,
        Unknow = 1,
        Initialize = 2,
        Standby = 4,
        OnMission = 8,
        MaintainMode = 16,
        Dock = 32,
        ErrorEvent = 64,
        Handshaking = 128
    }

    public enum MRStatus
    {
        None = 0,
        Unknow = 1, 
        Initialize = 2, 
        Idle = 4, 
        Docked = 8, 
        Busy = 16, 
        ForceDocked = 32,
        Stop = 64,
        Error= 128,
        Debug = 256,
        Moving = 512, 
        Handling = 1024, 
        Lost = 2048,
        MovingFail = 4096,
        Timeout = 8192,
        EMO = 16384,
        Bumper = 32768,
        Alarm = 65536
    }

	public partial class clsAGVInfo
	{
        private const string key_AGV = "AGVID=";
        private const string key_EQP = "EQP=";
        private const string key_IP = "IP=";
        private const string key_PORT = "PORT=";
        private const string key_EQPID = "EQPID=";
        private const string key_InputRack = "INPUTRACK=";
        private const string key_OutputRack = "OUTPUTRACK=";
        private const string key_RackID = "RACKID=";
        private const string key_Status = "STATUS=";
        private const string key_LotList = "LOTLIST=";
        private const string key_X = "XPOS=";
        private const string key_Y = "YPOS=";
        private const string key_split = ",";

        private string _AGVID = string.Empty;
        private clsToolList _ToolList = new clsToolList();
        private clsRackList _InputRack = new clsRackList();
        private clsRackList _OutputRack = new clsRackList();

        private MainStatus _MainStatus = MainStatus.None;
        private MRStatus _Status = MRStatus.None;
        private MRStatus _PreStatus = MRStatus.None;
        private List<clsAGVCommand> _AGV_Commands = new List<clsAGVCommand>();
        private bool _IsIdle = false;
        private string _XPos = string.Empty;
        private string _YPos = string.Empty;

        private string _IP = string.Empty;
        private int _Port = -1;
        private SocketConnection _socket = null;

        private bool _Is_Send_Load_1 = false;
        private bool _Is_Send_Load_2 = false;

        public clsAGVInfo()
        {
            this._AGVID = string.Empty;
            this._ToolList = new clsToolList();
            this._InputRack = new clsRackList();
            this._OutputRack = new clsRackList();
            this._Status = MRStatus.None;
            this._PreStatus = MRStatus.None;
            this._AGV_Commands = new List<clsAGVCommand>();
            this._IsIdle = false;
            this._XPos = string.Empty;
            this._YPos = string.Empty;
            this._IP = string.Empty;
            this._Port = -1;
            this._socket = null;
            this._Is_Send_Load_1 = false;
            this._Is_Send_Load_2 = false;
        }

        public string AGVID
        {
            get { return this._AGVID; }
            set { this._AGVID = value; }
        }
        public string IP
        {
            get { return this._IP; }
            set { this._IP = value; }
        }
        public int Port
        {
            get { return this._Port; }
            set { this._Port = value; }
        }
        public SocketConnection AGV_Socket
        {
            get { return this._socket; }
            set { this._socket = value; }
        }
        public clsToolList ToolList
        {
            get { return this._ToolList; }
            set { this._ToolList = value; }
        }
        public clsRackList InputRack
        {
            get { return this._InputRack; }
            set { this._InputRack = value; }
        }
        public clsRackList OutputRack
        {
            get { return this._OutputRack; }
            set { this._OutputRack = value; }
        }
        public List<clsAGVCommand> AGV_Commands
        {
            get { return this._AGV_Commands; }
            set { this._AGV_Commands = value; }
        }
        public bool IsIdle
        {
            get
            {
                bool result = this._IsIdle;
                if (result)
                {
                    if (this._AGV_Commands != null)
                    {
                        if (this._AGV_Commands.Count > 0)
                        {
                            result = false;
                        }
                    }
                }
                return result;
            }
        }
        public string X_Pos
        {
            get { return this._XPos; }
            set { this._XPos = value; }
        }
        public string Y_Pos
        {
            get { return this._YPos; }
            set { this._YPos = value; }
        }
        public bool Is_Send_Load_1
        {
            get { return this._Is_Send_Load_1; }
            set { this._Is_Send_Load_1 = value; }
        }
        public bool Is_Send_Load_2
        {
            get { return this._Is_Send_Load_2; }
            set { this._Is_Send_Load_2 = value; }
        }
        public void UpdateStatus(string mainStatus, string Status)
        {
            try
            {
                this._PreStatus = this._Status;
                switch (mainStatus.ToUpper())
                {
                    case "UNKNOW":
                	{
                        this._MainStatus = MainStatus.Unknow;
                		break;
                	}
                    case "INITIALIZE":
                    {
                        this._MainStatus = MainStatus.Initialize;
                        break;
                    }
                    case "STANDBY":
                    {
                        this._MainStatus = MainStatus.Standby;
                        break;
                    }
                    case "ONMISSION":
                    {
                        this._MainStatus = MainStatus.OnMission;
                        break;
                    }
                    case "MAINTAINMODE":
                    {
                        this._MainStatus = MainStatus.MaintainMode;
                        break;
                    }
                    case "DOCK":
                    {
                        this._MainStatus = MainStatus.Dock;
                        break;
                    }
                    case "ERROREVENT":
                    {
                        this._MainStatus = MainStatus.ErrorEvent;
                        break;
                    }
                    case "HANDSHAKING":
                    {
                        this._MainStatus = MainStatus.Handshaking;
                        break;
                    }                    
                	default :
                	{
                        this._MainStatus = MainStatus.None;
                		break;
                	}
                }
                switch (Status.ToUpper())
                {
                    case "UNKNOW":
                    {
                        this._Status = MRStatus.Unknow;
                        break;
                    }
                    case "INITIALIZATION":
                    {
                        this._Status = MRStatus.Initialize;
                        break;
                    }
                    case "STANDBY":
                    {
                        this._Status = MRStatus.Idle;
                        break;
                    }
                    case "DOCKED":
                    {
                        this._Status = MRStatus.Docked;
                        break;
                    }
                    case "BUSY":
                    {
                        this._Status = MRStatus.Busy;
                        break;
                    }
                    case "FORCEDOCKED":
                    {
                        this._Status = MRStatus.ForceDocked;
                        break;
                    }
                    case "STOP":
                    {
                        this._Status = MRStatus.Stop;
                        break;
                    }
                    case "ERROR":
                    {
                        this._Status = MRStatus.Error;
                        break;
                    }
                    case "DEBUG":
                    {
                        this._Status = MRStatus.Debug;
                        break;
                    }
                    case "MOVING":
                    {
                        this._Status = MRStatus.Moving;
                        break;
                    }
                    case "HANDLING":
                    {
                        this._Status = MRStatus.Handling;
                        break;
                    }
                    case "LOST":
                    {
                        this._Status = MRStatus.Lost;
                        break;
                    }
                    case "MOVINGFAIL":
                    {
                        this._Status = MRStatus.MovingFail;
                        break;
                    }
                    case "TIMEOUT":
                    {
                        this._Status = MRStatus.Timeout;
                        break;
                    }
                    case "EMO":
                    {
                        this._Status = MRStatus.EMO;
                        break;
                    }
                    case "BUMPER":
                    {
                        this._Status = MRStatus.Bumper;
                        break;
                    }
                    case "ALARM":
                    {
                        this._Status = MRStatus.Alarm;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                Check_Is_Idle();
            }
            catch (System.Exception ex)
            {	
            }
        }

        private void Check_Is_Idle()
        {
            try
            {
                switch (this._MainStatus)
                {
                    //case MainStatus.Dock:
                    case MainStatus.Standby:
                    {
                        switch (this._Status)
                        {
                            case MRStatus.Docked:
                            case MRStatus.Idle:
                            {
                                this._IsIdle = true;                                
                                break;
                            }
                            default:
                            {
                                this._IsIdle = false;
                                break;
                            }
                        }
                        break;
                    }
                    default:
                    {
                        this._IsIdle = false;
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
                this._IsIdle = false;
            }
        }

        public MRStatus Pre_Status
        {
            get { return this._PreStatus; }
        }

        public MRStatus AGVStatus
        {
            get { return this._Status; }
            set 
            { 
                this._Status = value;
                Check_Is_Idle();
            }
        }

        public MainStatus AGVMainStatus
        {
            get { return this._MainStatus; }
            set 
            { 
                this._MainStatus = value;
                Check_Is_Idle();
            }
        }

        public clsLotInfo FindLotByLotID(string LotID)
        {
            clsLotInfo result = null;
            try
            {
            	foreach (clsAGVCommand agvcmd in this._AGV_Commands)
            	{
            	    foreach (clsLotInfo lot in agvcmd.LotList.Values)
            	    {
            	        if (lot.LotID == LotID)
            	        {
                            result = lot;
                            return result;
            	        }
            	    }
            	}
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }

        public Dictionary<string, clsLotInfo> FindLotsByBatchID(string BatchID)
        {
            Dictionary<string, clsLotInfo> result = new Dictionary<string, clsLotInfo>();
            try
            {
                foreach (clsAGVCommand agvcmd in this._AGV_Commands)
                {
                    foreach (clsLotInfo lot in agvcmd.LotList.Values)
                    {
                        if (lot.Batch == BatchID)
                        {
                            try
                            {
                                result.Add(lot.LotID, lot);
                            }
                            catch (System.Exception ex)
                            {	
                            }                            
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }

        public Dictionary<string, clsLotInfo> FindMovingLots()
        {
            Dictionary<string, clsLotInfo> result = new Dictionary<string, clsLotInfo>();
            try
            {
                foreach (clsAGVCommand agvcmd in this._AGV_Commands)
                {
                    foreach (clsLotInfo lot in agvcmd.LotList.Values)
                    {
                        try
                        {
                            result.Add(lot.LotID, lot);
                        }
                        catch (System.Exception ex)
                        {
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                result = new Dictionary<string, clsLotInfo>();
            }
            return result;
        }

        public string ToObjectString()
        {
            string result = string.Empty;
            try
            {
                result += "[";
                result += key_AGV + this._AGVID + key_split;
                result += key_IP + this._IP + key_split;
                result += key_PORT + this._Port.ToString() + key_split;
                result += key_EQP + "[";
                foreach (clsToolInfo tool in this._ToolList.ToolList.Values)
                {
                    result += key_EQPID + tool.ToolID + key_split;
                }
                result = result.Trim(key_split.ToCharArray()) + "]";
                result += key_split;
                result += key_InputRack + "[";
                foreach (clsRackInfo rack in this._InputRack.RackList.Values)
                {
                    result += key_RackID + rack.Name + key_split;
                }
                result = result.Trim(key_split.ToCharArray()) + "]";
                result += key_split;
                result += key_OutputRack + "[";
                foreach (clsRackInfo rack in this._OutputRack.RackList.Values)
                {
                    result += key_RackID + rack.Name + key_split;
                }
                result = result.Trim(key_split.ToCharArray()) + "]";                
                result += "]";
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public override string ToString()
        {
            return this._AGVID;
        }
	}
}
